/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.imagepipeline.memory;

import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.Sets;
import com.facebook.common.memory.MemoryTrimType;
import com.facebook.imageutils.BitmapUtil;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import java.util.Set;

public class DummyTrackingInUseBitmapPool implements BitmapPool {

    /**
     * An Identity hash-set to keep track of values by reference equality
     */
    private final Set<PixelMap> mInUseValues;

    public DummyTrackingInUseBitmapPool() {
        mInUseValues = Sets.newIdentityHashSet();
    }

    @Override
    public void trim(MemoryTrimType trimType) {
        // nop
    }

    @Override
    public PixelMap get(int size) {
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.size.width = 1;
        options.size.height = (int) Math.ceil(size / (double) BitmapUtil.RGB_565_BYTES_PER_PIXEL);
        options.pixelFormat = PixelFormat.RGB_565;
        PixelMap result = PixelMap.create(options);
        mInUseValues.add(result);
        return result;
    }

    @Override
    public void release(PixelMap value) {
        Preconditions.checkNotNull(value);
        mInUseValues.remove(value);
        value.release();
    }
}
